﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MetaProperties.Core.EventFilters
{
    using System.Threading;

    using Framework;

    /// <summary>
    /// Dispatching event filter.
    /// </summary>
    public class DispatchingEventFilter : DispatchingEventFilterBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DispatchingEventFilter"/> class.
        /// </summary>
        public DispatchingEventFilter()
                : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DispatchingEventFilter"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public DispatchingEventFilter(ISynchronizationContextContainer context)
                : base(context)
        {
        }

        /// <summary>
        /// Resumes the event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <param name="pipeline">The pipeline.</param>
        protected override void Resume(object sender, EventArgs args, IFilterPipeline pipeline)
        {
            SynchronizationContext syncContext = this.SynchronizationContextContainer.SynchronizationContext;
            if (syncContext == null)
            {
                throw new Exception("The synchronization context container didn't contain a synchronization context.");
            }
            
            syncContext.Post(
                    o => pipeline.Resume(sender, args),
                    null);
        }
    }
}